﻿using DTO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using System.Globalization;
using System.Text.RegularExpressions;

namespace QuanLyRapChieuPhim
{
    public partial class frmQuanLyGiaVe : Form
    {
        private List<DinhDangPhim> dsDinhDangPhim;
        private List<SuatChieu> dsXuatChieu;
        private BindingList<BangGiaVe> dsGiaVe;
        public frmQuanLyGiaVe()
        {
            InitializeComponent();
        }
        
        private void frmQuanLyGiaVe_Load(object sender, EventArgs e)
        {
            dsGiaVe = QuanLyGiaVeBUS.getDSGiaVe();
            loadDDPhim();
            loadXuatChieu();
            formatDGVBangGia();
            dgvBangGia.DataSource = dsGiaVe;
            
        }

        private void formatDGVBangGia()
        {
            dgvBangGia.AllowUserToAddRows = false;
            dgvBangGia.RowHeadersVisible = false;
            dgvBangGia.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            dgvBangGia.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(200, 220, 255);
            dgvBangGia.AutoGenerateColumns = false;

            colDDPhim.DataSource = dsDinhDangPhim;
            colDDPhim.DisplayMember = "TenDinhDangPhim";
            colDDPhim.ValueMember = "MaDinhDangPhim";

            colXuatChieu.DataSource = dsXuatChieu;
            colXuatChieu.ValueMember = "MaSuatChieu";
            colXuatChieu.DisplayMember = "TenSuatChieu";
        }

        private void loadXuatChieu()
        {
            dsXuatChieu = QuanLyGiaVeBUS.getDsXuatChieu();
            cbXuatChieu.DataSource = dsXuatChieu;
            cbXuatChieu.ValueMember = "MaSuatChieu";
            cbXuatChieu.DisplayMember = "TenSuatChieu";
        }

        

        private void loadDDPhim()
        {
            dsDinhDangPhim = PhongChieuBUS.layDSDieuKienKT();
            cbbDinhDangPhim.DataSource = dsDinhDangPhim;
            cbbDinhDangPhim.ValueMember = "MaDinhDangPhim";
            cbbDinhDangPhim.DisplayMember = "TenDinhDangPhim";
        }

        private void cbbDinhDangPhim_SelectedIndexChanged(object sender, EventArgs e)
        {
           // int maDinhDang = cbbDinhDangPhim.SelectedValue;
            if (cbbDinhDangPhim.SelectedValue.GetType() == typeof(int) 
                && cbXuatChieu.SelectedValue.GetType() == typeof(string))
            {
                int maDD = (int)cbbDinhDangPhim.SelectedValue;
                string maXC = (string)cbXuatChieu.SelectedValue;
                foreach (BangGiaVe bg in dsGiaVe)
                {
                    if (bg.MaDinhDang == maDD && bg.MaSuatChieu.CompareTo(maXC) == 0)
                    {
                        txtGiaVe.Text = bg.GiaVe.ToString("N0");
                        txtGiaVe.Tag = bg.MaGiaVe;
                        return;
                    }
                    else
                    {
                        txtGiaVe.Text = "";
                        txtGiaVe.Tag = -1;
                    }
                }
            }
            txtGiaVe.Text = "";
            txtGiaVe.Tag = -1;
        }

        private void btnRefearch_Click(object sender, EventArgs e)
        {
            dsGiaVe = QuanLyGiaVeBUS.getDSGiaVe();
            dgvBangGia.DataSource = dsGiaVe;
        }

        private void btntSave_Click(object sender, EventArgs e)
        {
            BangGiaVe gv = new BangGiaVe();
            gv.MaDinhDang = (int)cbbDinhDangPhim.SelectedValue;
            gv.MaSuatChieu = cbXuatChieu.SelectedValue.ToString();
            Match match = Regex.Match(txtGiaVe.Tag.ToString(), @"GV([0-9\-]+)", RegexOptions.IgnoreCase);
            if (float.Parse(txtGiaVe.Text.ToString()) > 0 && match.Success == true)
            {
                gv.GiaVe = float.Parse(txtGiaVe.Text.ToString());
                gv.MaGiaVe = txtGiaVe.Tag.ToString();
                string kq = QuanLyGiaVeBUS.capNhatGiaVe(gv);
                int num;
                bool isNum = Int32.TryParse(kq, out num);
                if (isNum == true && int.Parse(kq) != 0)
                {
                    MessageBox.Show("Cập nhật giá vé thành công");
                    dsGiaVe = QuanLyGiaVeBUS.getDSGiaVe();
                    dgvBangGia.DataSource = dsGiaVe;
                }
                else
                {
                    MessageBox.Show(kq);
                }
            }
            else if (float.Parse(txtGiaVe.Text.ToString()) > 0 && float.Parse(txtGiaVe.Tag.ToString()) == -1)
            {
                gv.GiaVe = float.Parse(txtGiaVe.Text.ToString());
                string kq = QuanLyGiaVeBUS.themGiaVe(gv);
                int num;
                bool isNum = Int32.TryParse(kq, out num);
                if (isNum == true && int.Parse(kq) != 0)
                {
                    MessageBox.Show("Thêm giá vé thành công");
                    dsGiaVe = QuanLyGiaVeBUS.getDSGiaVe();
                    dgvBangGia.DataSource = dsGiaVe;
                }
                else
                {
                    MessageBox.Show(kq);
                }
            }
            else
            {
                MessageBox.Show("Giá vé không được nhỏ hơn 0");
            }
        }
        
        private void txtGiaVe_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) == false && char.IsControl(e.KeyChar) == false)
            {
                e.Handled = true;
            }
        }

        private void txtGiaVe_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (char.IsDigit(char.Parse(e.KeyValue.ToString())) == false)
                {
                    e.Handled = true;
                }
            }
            catch (System.Exception )
            {
            }
        }

        private void dgvBangGia_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                BangGiaVe gv = dsGiaVe[e.RowIndex];
                cbbDinhDangPhim.SelectedValue = gv.MaDinhDang;
                cbXuatChieu.SelectedValue = gv.MaSuatChieu;
            }
        }
     }
}
